/* Archive Extractor
 * Copyright 2008-2009 Matthew Hoops
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * $URL: http://archive-extractor.googlecode.com/svn/trunk/d64.cpp $
 * $Id: d64.cpp 2 2009-03-23 01:29:43Z clone2727 $
 *
 */

#include "d64.h"

D64Image::D64Image() : StandardArchive() {
}

std::vector<const char*> D64Image::getArchiveExtensions() {
	std::vector<const char*> exList;
	exList.push_back("d64");
	return exList;
}

static const byte sectorCount[] = {
	// Track 0 (Does not exist!)
	0, 
	// Tracks 1-17
	21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
	// Tracks 18-24
	19, 19, 19, 19, 19, 19, 19, 
	// Tracks 25-30
	18, 18, 18, 18, 18, 18,
	// Tracks 31-35
	17, 17, 17, 17, 17, 
	// Tracks 36-40
	17, 17, 17, 17, 17,
	// Tracks 41-42
	17, 17
};

struct BlockAllocationMap {
	uint16 trackSectorDirStart;
	byte dosVersion;
	byte reserved0;
	byte bamEntries[42][4];
	char diskName[16];
	uint16 reserved1;
	uint16 diskID;
	byte reserved2;
	uint16 dosType;
	uint16 reserved3;
};

struct D64Directory {
	uint16 trackSector;
	byte fileType;
	uint16 trackSectorFirstFile;
	char fileName[16];
	uint16 trackSectorFirstSideSector;
	byte reserved[6];
	uint16 fileSize;
};

void D64Image::seekToTrack(FILE* handle, byte track) {
	fseek(handle, 0, SEEK_SET);
	// One Based!
	for (byte i = 1; i < track; i++)
		fseek(handle, sectorCount[i] * 256, SEEK_CUR);
}

// Convert from PETASCII to ASCII
static char convertChar(byte c) {
	if (c >=   1 && c <=  26)
		return (char)(c + 64);
	if (c >= 129 && c <= 154)
		return (char)(c - 64);
	if (c >= 192 && c <= 218)
		return (char)(c - 128);
	return (char)c;
}

bool D64Image::extractFiles(FILE* file) {
	uint32 fileSize = getFileSize(file);
	if (fileSize != 174848 ||
		fileSize != 175531 ||
		fileSize != 196608 ||
		fileSize != 197376)
		return false;
	byte trackCount = (fileSize == 174848 || fileSize == 175531) ? 35 : 40;
	//uint16 errorCount = (fileSize == 174848 || fileSize == 175531) ? 683 : 768;
	
	// Now, we need to get to track 18 (BAM + Directory)
	seekToTrack(file, 18); 
	
	// Read in the BAM
	BlockAllocationMap bam;
	bam.trackSectorDirStart = readUint16LE(file);
	bam.dosVersion = readByte(file);
	bam.reserved0 = readByte(file);
	// 35 entries here, more later
	for (byte i = 0; i < 35; i++)
		for (byte j = 0; j < 4; j++)
			bam.bamEntries[i][j] = readByte(file);
	fread(bam.diskName, 1, 16, file);
	bam.reserved1 = readUint16LE(file);
	bam.diskID = readUint16LE(file);
	bam.reserved2 = readByte(file);
	bam.dosType = readUint16LE(file);
	bam.reserved3 = readUint16LE(file);
	if (trackCount == 40) {
		for (byte i = 0; i < 5; i++)
			for (byte j = 0; j < 4; j++)
				bam.bamEntries[i + 35][j] = readByte(file);
		fseek(file, 65, SEEK_CUR);
	} else if (trackCount == 42) {
		for (byte i = 0; i < 7; i++)
			for (byte j = 0; j < 4; j++)
				bam.bamEntries[i + 35][j] = readByte(file);
		fseek(file, 57, SEEK_CUR);
	} else {
		fseek(file, 85, SEEK_CUR);
	}
	
	// Read in the directories
	D64Directory* directoryIndices = new D64Directory[trackCount];
	for (byte i = 0; i < trackCount; i++) {
		directoryIndices[i].trackSector = readUint16LE(file);
		directoryIndices[i].fileType = readByte(file);
		directoryIndices[i].trackSectorFirstFile = readUint16LE(file);
		for (byte j = 0; j < 16; j++)
			directoryIndices[i].fileName[j] = convertChar(readByte(file));
		directoryIndices[i].trackSectorFirstSideSector = readUint16LE(file);
		fread(directoryIndices[i].reserved, 1, 6, file);
		directoryIndices[i].fileSize = readUint16LE(file);
	}
	
	for (byte i = 0; i < trackCount; i++) {
		if (!directoryIndices[i].fileSize)
			continue;
		if (!directoryIndices[i].fileName[0])
			sprintf(directoryIndices[i].fileName, "FILE.%03d", i);
		printf ("Extracting file \'%s\'\n", directoryIndices[i].fileName);
		// TODO: Actual Extraction
	}
	
	return true;
}
